package io.rong.imkit.http

import android.content.Context
import com.custom.base.base.SDEventManager
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import io.rong.imkit.API
import io.rong.imkit.SpName
import io.rong.imkit.entity.BaseEntity
import io.rong.imkit.entity.GreetingFlirtingEntity
import io.rong.imkit.entity.GreetingMessageEntity
import io.rong.imkit.entity.OpenChatEntity
import io.rong.imkit.entity.PrivateAlbumEntity
import io.rong.imkit.entity.UserProfileEntity
import io.rong.imkit.event.EnumEventTag
import io.rong.imkit.event.FirebaseEventTag
import io.rong.imkit.pay.IMRegidterPayListener
import io.rong.imkit.userinfo.RongUserInfoManager
import io.rong.imkit.utils.FirebaseEventUtils


object HttpRequest {
    fun getMemberReduce(
        context: Context,
        chatUserCode: String,
        callback: RequestCallBack,
        benefitCode: Int,
        type: Int,
        imageCode: String,
        uid: String? = null
    ) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_reduce_benefits_url)
                requestBody.add("benefitCode", benefitCode)
                requestBody.add("chatUserCode", chatUserCode)
                requestBody.add("imageCode", imageCode)
                uid?.let { requestBody.add("msgId", it) }

            }
        }, object : SDOkHttpResoutCallBack<BaseEntity>() {
            override fun onSuccess(entity: BaseEntity) {
                callback.onSuccess()
            }

            override fun onFailure(code: Int, msg: String) {
                when (code) {
                    2002 -> {
                        FirebaseEventUtils.logEvent(if (type == 3) FirebaseEventTag.Chat_PP_Buy.name else FirebaseEventTag.Chat_PV_Buy.name)
                        IMRegidterPayListener.listener.goBuyMemberUnitaryPay(context, type, type) {
                            FirebaseEventUtils.logEvent(if (type == 3) FirebaseEventTag.Chat_PP_Buysuccess.name else FirebaseEventTag.Chat_PV_Buysuccess.name)
                            getMemberReduce(context, chatUserCode, callback, benefitCode, type, imageCode)
                        }
                    }

                    2003 -> {
                        FirebaseEventUtils.logEvent(if (type==3)FirebaseEventTag.Chat_PP_Sub.name else FirebaseEventTag.Chat_PV_Sub.name)
                        IMRegidterPayListener.listener.goBuyMemberPay(context, type, 1,chatUserCode) {
                            FirebaseEventUtils.logEvent(if (type==3)FirebaseEventTag.Chat_PP_Subsuccess.name else FirebaseEventTag.Chat_PV_Subsuccess.name)
                            BaseConfig.getInstance.setBoolean(SpName.isMember, true)
                        }
                    }
                }
            }
        })
    }

    fun showMemberDialog(context: Context, callback: RequestSuccessCallBack) {//targetId 未知
        FirebaseEventUtils.logEvent(FirebaseEventTag.Chat_PrivateAlbum_Sub.name)
        IMRegidterPayListener.listener.goBuyMemberPay(context, 5, 1,"") {
            FirebaseEventUtils.logEvent(FirebaseEventTag.Chat_PrivateAlbum_Subsuccess.name)
            BaseConfig.getInstance.setBoolean(SpName.isMember, true)
            callback.onSuccess()
        }
    }

    fun getUserInfo(callback: RequestUserInfoCallBack) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_info_url)
            }
        }, object : SDOkHttpResoutCallBack<UserProfileEntity>() {
            override fun onSuccess(entity: UserProfileEntity) {
                callback.onSuccess(entity)
            }
        })
    }

    fun getUserInfoEmpty(
        context: Context,
        callback: RequestSuccessCallBack
    ) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_info_url)
            }
        }, object : SDOkHttpResoutCallBack<UserProfileEntity>() {
            override fun onSuccess(entity: UserProfileEntity) {
                if (entity.data.isMember) {
                    callback.onSuccess()
                } else {
                    IMRegidterPayListener.listener.goBuyMemberPay(context, 5, 1,"") {
                        BaseConfig.getInstance.setBoolean(SpName.isMember, true)
                    }
                }
            }
        })
    }

    fun getPrivateAlbum(callBack: RequestPrivateAlbumCallBack) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_user_albums_url)
                requestBody.add("type", 3)
            }

        }, object : SDOkHttpResoutCallBack<PrivateAlbumEntity>() {
            override fun onSuccess(entity: PrivateAlbumEntity) {
                callBack.onSuccess(entity)
            }
        })
    }

    fun getTrySendMessage(toUserId: String, context: Context, callback: RequestCallBack) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_send_message_url)
                requestBody.add("toUserId", toUserId)
            }

        }, object : SDOkHttpResoutCallBack<BaseEntity>() {
            override fun onSuccess(entity: BaseEntity) {
                callback.onSuccess()
            }

            override fun onFailure(code: Int, msg: String) {
                super.onFailure(code, msg)
                when (code) {
                    3004, 3007, 3008, 3009 -> {
                        callback.onFailure(code, msg)
                        IMRegidterPayListener.listener.goBuyMemberPay(context, 0, 1,toUserId) {
                            BaseConfig.getInstance.setBoolean(SpName.isMember, true)
                        }
                    }
                }
            }
        })
    }

    fun addAlbums(path: String, videoLength: Long) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_user_albums_add_url)
                requestBody.add("imageUrl", path)
                requestBody.add("type", 3)
                requestBody.add("videoLength", videoLength)

            }
        }, object : SDOkHttpResoutCallBack<BaseEntity>() {
            override fun onSuccess(entity: BaseEntity) {
            }

            override fun onFailure(code: Int, msg: String) {
            }
        })
    }

    fun chatOpen(targetId: String, type: Int, callback: RequestTurnOnsSuccessCallBack) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_chat_open_url)
                requestBody.add("userCodeFriend", targetId)
                requestBody.add("type", type)
                if (type == 2) {
                    requestBody.add(
                        "sendMessagesNumber",
                        BaseConfig.getInstance.getInt("${targetId}_sendMessagesNumber", 0)
                    )
                }

            }
        }, object : SDOkHttpResoutCallBack<OpenChatEntity>() {
            override fun onSuccess(entity: OpenChatEntity) {
                if (type == 1) {
                    BaseConfig.getInstance.setInt(
                        SpName.conversationList + targetId,
                        entity.data.onlineStatus
                    )
                    BaseConfig.getInstance.setInt(
                        SpName.conversationFlashList + targetId,
                        entity.data.flashchatFlag
                    )

                    BaseConfig.getInstance.setInt("${targetId}_sendMessagesNumber", 0)
                    BaseConfig.getInstance.setInt(
                        "${targetId}_messagesNumber",
                        entity.data.messagesNumber
                    )
                    BaseConfig.getInstance.setBoolean("${targetId}_member", entity.data.member)
                    callback.onSuccess(entity.data)
                } else {
                    if (entity.data.effectiveChat) {
                        val userInfo = RongUserInfoManager.getInstance().getUserInfo(targetId)
                        SDEventManager.post(
                            userInfo.name,
                            EnumEventTag.SHOW_NOTIFICATION_DIALOG.ordinal
                        )
                    }
                }
            }

            override fun onFailure(code: Int, msg: String) {
            }
        })
    }

    interface RequestCallBack {
        fun onSuccess()
        fun onFailure(code: Int, msg: String)
    }

    interface RequestSuccessCallBack {
        fun onSuccess()
    }

    interface RequestTurnOnsSuccessCallBack {
        fun onSuccess(entity: OpenChatEntity.Data)
    }

    interface RequestUserInfoCallBack {
        fun onSuccess(entity: UserProfileEntity)
    }

    interface RequestPrivateAlbumCallBack {
        fun onSuccess(entity: PrivateAlbumEntity)
    }


    fun showBuyMember(context: Context, type: Int,targetId:String) {
        IMRegidterPayListener.listener.goBuyMemberPay(context, type, 1,targetId) {
            BaseConfig.getInstance.setBoolean(SpName.isMember, true)
        }
    }


    fun commonNotify(type: Int, content: String) {
        try {
            if (BaseConfig.getInstance.getBoolean(SpName.openNotify, true)) {
                OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
                    override fun addBody(requestBody: OkHttpBodyEntity) {
                        requestBody.setPost(API.user_common_notify_url)
                        requestBody.add("code", type)
                        requestBody.add("content", content)
                        requestBody.add(
                            "userCode",
                            BaseConfig.getInstance.getString("userCode", "")
                        )
                    }
                }, object : SDOkHttpResoutCallBack<BaseEntity>(false) {
                    override fun onSuccess(entity: BaseEntity) {
                    }
                })
            }
        } catch (e: Exception) {

        }

    }

    fun getGreetingFlirting(callback: (result: GreetingFlirtingEntity) -> Unit){
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setGet(API.greeting_flirting_url)
            }
        }, object : SDOkHttpResoutCallBack<GreetingFlirtingEntity>() {
            override fun onSuccess(entity: GreetingFlirtingEntity) {
                callback.invoke(entity)
            }

            override fun onFailure(code: Int, msg: String) {
            }
        })
    }

    fun getGreetingMessage(callback: (result: GreetingMessageEntity) -> Unit?){
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setGet(API.greeting_message_url)
            }
        }, object : SDOkHttpResoutCallBack<GreetingMessageEntity>() {
            override fun onSuccess(entity: GreetingMessageEntity) {
                callback.invoke(entity)
            }

            override fun onFailure(code: Int, msg: String) {
            }
        })
    }

}